/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Util;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Fulbert
 */
public class utilErrorHandler {
    
    public static void printError(String errMsg, String panelName, String errorLocation) {
        
        try {
            System.out.print(errMsg+ "\n");
            JOptionPane.showMessageDialog(null, errMsg.substring(errMsg.lastIndexOf(':') + 1, errMsg.length()) + "\nError location:" + panelName + ":" + errorLocation, errMsg.substring(0, errMsg.lastIndexOf(':')), JOptionPane.ERROR_MESSAGE);
        } catch(Exception e) {
            JOptionPane.showMessageDialog(null, errMsg, "Error", JOptionPane.ERROR_MESSAGE);
        }
        //Log the complete error to the log file
        
        TimeZone tz = TimeZone.getTimeZone("GMT-8"); 
        Date now = new Date();
        DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss ");
        df.setTimeZone(tz);
        String currentTime = df.format(now);
        
        FileWriter aWriter;
        
        try {
            aWriter = new FileWriter("./ErrorLog.log", true);
            aWriter.write(currentTime + ":" + errMsg + "\n");
            aWriter.flush();
            aWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(utilErrorHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
}
